#include"common_headers.h"
int makedir31(char parent_path[300],char dir_name[100])
{

	int is_mounted=0, is_valid_dir_name=0, length_of_path=0;
	char dir_path_testing[300], filenameFromfilepath[30], name[30];
	char *result;
	char delims[]="/";
	char str[300], filename[50], parent_path_copy[300];
	
	
	strcpy(parent_path_copy, parent_path);

	//CHECK IF VFS MOUNTED
	is_mounted = validateMountState();
	if(is_mounted)
	{
		//CHECK IF FILE SYSTEM NOT FULL
		if(mainHeader.usedFileDescriptors < mainHeader.maximumFileDescriptor)
		{
			//CHECK IF FILE SYSTEM FULL OR NOT
			int m;
		    for(m=0; m<mainHeader.maximumFileDescriptor; m++)
		    {
		    	if(freelist[m]==0)
		        {
		            mainHeader.usedFileDescriptors++;
		            freelist[m]=1;
		            struct FileDescriptor *fd_Desc1;
		            fd_Desc1=&fileDescriptors[m];

		            strcpy(fd_Desc1->filename,dir_name);
		            strcpy(fd_Desc1->filetype,"");// not updating location block number and file type*************************
		            fd_Desc1->filesize=0;
		          
		            length_of_path = strlen(parent_path_copy);
		            if(parent_path_copy[length_of_path - 1]!='/')
		            {
		            	strcat(parent_path,"/");
		            }
		           
		            strcat(parent_path,dir_name);
		            strcpy(fd_Desc1->locationpath,parent_path);
		            char dir[300];
		            int y=0,q=0,j=0;
		            dir[q]=fd_Desc1->locationpath[q];//for '/'
		            while(q<(strlen(fd_Desc1->locationpath)-1))
		            {
		                if(fd_Desc1->locationpath[q]=='/' && fd_Desc1->locationpath[q+1]=='/')
		                {
		                	//INVALID CHARACTER IN DIR_NAME
		                    printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_02);
		                    mainHeader.usedFileDescriptors--;
		                    freelist[m]=0;
		                    return 0;
		                }
		                q++;
		            }
		            q=0;
		            q++;

		            while(q<strlen(fd_Desc1->locationpath))
		            {
		            
		            	
		            	
		            	strcpy(dir_path_testing, fd_Desc1->locationpath);
				
						//EXTRACTING DIRNAME FROM SOURCE FILE PATH					
						result = strtok(dir_path_testing, delims);
						while(result!=NULL)
						{
							strcpy(filenameFromfilepath, result);
							is_valid_dir_name = validateDirectoryName(filenameFromfilepath);
							if(!is_valid_dir_name)
							{
								printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_02);
								return 0;	
							}
							
							
							result=strtok(NULL, delims);
						}
						strcpy(name,filenameFromfilepath);	//DIRNAME EXTRACTED IN "name" VARIABLE
						
						//CHECK IF SOURCE DIR NAME IS A TEXT FILE
						is_valid_dir_name = validateDirectoryName(name);
						if(!is_valid_dir_name)
							return 0;	
		            	
		            	


		                while(fd_Desc1->locationpath[q]!='/' && q<strlen(fd_Desc1->locationpath))
		                {
		                
		                
		                
		                	
		                
		                
		                    if(!((fd_Desc1->locationpath[q]>='a' && fd_Desc1->locationpath[q]<='z') || (fd_Desc1->locationpath[q]>='A' &&
		                            fd_Desc1->locationpath[q]<='Z') || (fd_Desc1->locationpath[q]>='0' && fd_Desc1->locationpath[q]<='9') || fd_Desc1->locationpath[q]=='_' || fd_Desc1->locationpath[q]=='-'))
		                    {
		                    	//INVALID CHARACTER IN DIR_NAME
		                        //printf("\nmakedir_FAILURE %s",ERR_VFS_MAKEDIR_02);
		                        //mainHeader.usedFileDescriptors--;
		                        //freelist[m]=0;
		                        //return 0;
		                    }
		                    dir[q]=fd_Desc1->locationpath[q];
		                    q++;
		                }
		                dir[q]='\0';


		                y=search_nary(dir);
		                if(y==0)
		                {
		                    if(q==strlen(fd_Desc1->locationpath))//only this node/fd needs to be created
		                    {
		                        j=insert_nary(fd_Desc1);


		                        //insert_Into_HashTable(fd_Desc1);
		                        ht_start= hash_insert(ht_start, dir_name);
		                        
		                        

		                        BSTInsert(fd_Desc1 );


		                    }
		                    else//intermediate nodes/fds also need to be created
		                    {
		                        if(mainHeader.usedFileDescriptors<mainHeader.maximumFileDescriptor)
		                        {
		                            int n;
		                            for(n=0; n<mainHeader.maximumFileDescriptor; n++)
		                            {
		                                if(freelist[n]==0)
		                                {
		                                    mainHeader.usedFileDescriptors++;
		                                    freelist[n]=1;
		                                    struct FileDescriptor *fd2;
		                                    fd2=&fileDescriptors[n];
		                                    n=mainHeader.maximumFileDescriptor;
		                                    strcpy(fd2->locationpath,dir);
		                                    strcpy(fd2->filetype,"");
		                                    fd2->filesize=0;
		                                    int length=strlen(fd2->locationpath),slashposition,remaining;
		                                    int extra=length,need;
		                                    while(extra>=0)
		                                    {
		                                        if(fd2->locationpath[extra]=='/')
		                                        {
		                                            slashposition=extra;
		                                            extra=0;
		                                        }
		                                        extra--;
		                                    }
		                                    remaining=length-slashposition;
		                                    char extra1[300];
		                                    for(need=0; need<remaining; need++)
		                                    {
		                                        extra1[need]=fd2->locationpath[need+slashposition+1];
		                                    }
		                                    strcpy(fd2->filename,extra1);



		                                    j=insert_nary(fd2);


		                                    //_Into_HashTable(fd2);
		                                    
		                                    ht_start= hash_insert(ht_start, dir_name);
		                                    // call insertinto bst here
		                                    BSTInsert(fd2 );

		                                }
		                            }
		                        }
		                        else
		                        {
		                            printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_01);
		                            mainHeader.usedFileDescriptors--;
		                            freelist[m]=0;
		                            return 0;
		                        }
		                    }
		                }
		                else if(y==1 && q==strlen(fd_Desc1->locationpath))
		                {
		                	//DIRECTORY ALREADY EXISTS
		                    printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_03);
		                    mainHeader.usedFileDescriptors--;
		                    freelist[m]=0;
		                    return 0;
		                }


		                if(fd_Desc1->locationpath[q]=='/')
		                {
		                    dir[q]=fd_Desc1->locationpath[q];
		                    q++;
		                }



		            }

		            if(j==1 || y==1)
		            {
		                printf("makedir_SUCCESS\n");
		                return 1;
		            }
		            else if(j!=1 && y!=1) return 0;
		            m=mainHeader.maximumFileDescriptor;// to break out of loop in next iteration

		        }	    
		    }		
		}
		else
		{
			//FILESYSTEM_FULL
			printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_01);
		}		
	}
	else
	{
		//VFS_NOT_MOUNTED
		printf("makedir_FAILURE %s\n",ERR_VFS_MAKEDIR_05);
	}  
	return 0;  
}
